package han.mutual;

import com.han.http.fetch.Fetcher;
import han.entity.FinanceProjectInfoEntity;
import han.entity.InvestCompanyInfoEntity;
import han.sql.Operator;
import han.util.ExcelUtils;

import java.util.*;

/**
 * @author: Han
 * @date: 2018/7/4 15:34
 * @dscription: TODO
 */
public class Exporter {

    private Operator operator;

    private Exporter(boolean b) {
        Fetcher fetcher = new Fetcher();
        operator = Operator.getInstance();
    }

    public Exporter() {
    }

    public static void main(String... args) {
        Exporter exporter = new Exporter(true);
        exporter.exportProjectInfo();
    }

    private static void t() {
        /*System.out.print("请输入数组大小:");
        Scanner scan = new Scanner(System.in);
        int[] arr = new int[scan.nextInt()];
        System.out.println("请输入数组内容:");
        for (int i = 0;i < arr.length ;i++ )
        {
            arr[i] = scan.nextInt();
        }*/
        int[] arr = {5, 3, 78, 1, 2};
        int key;
        int c;
        for (int j = 1; j < arr.length; j++) {
            key = arr[j];
            c = j - 1;
            while (c > 0 && arr[j] > key) {
                arr[c + 1] = arr[c];
                c = c - 1;
            }
        }
        for (int i : arr) {
            System.out.print(i + ",");
        }
    }

    private void exportProjectInfo() {
        List<FinanceProjectInfoEntity> baseInfoEntityList = operator.queryProjectInfoByCondition(1);
        List<Map<String, String>> infoEntityList = new ArrayList<>();
        Map<String, List<Map<String, String>>> sheetMap = new LinkedHashMap<>();
        for (FinanceProjectInfoEntity infoEntity : baseInfoEntityList) {
            try {
                Map<String, String> map = infoEntity.generateMap();
                infoEntityList.add(map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sheetMap.put("sheet1", infoEntityList);
        ExcelUtils.writeToLocal("F:\\融资项目信息.xls", sheetMap);
    }

    private void exportCompanyInfo() {
        List<InvestCompanyInfoEntity> entityList = operator.queryAllCompanyInfo();
        List<Map<String, String>> infoEntityList = new ArrayList<>();
        List<Map<String, String>> longInfoEntityList = new ArrayList<>();
        Map<String, List<Map<String, String>>> sheetMap = new LinkedHashMap<>();
        for (InvestCompanyInfoEntity entity : entityList) {
            Map<String, String> tempMap = entity.generateMap();
            if (tempMap.size() <= 254) {
                infoEntityList.add(tempMap);
            } else {
                List<Map<String, String>> tempList = splitMap(tempMap);
                longInfoEntityList.addAll(tempList);
            }
        }
        infoEntityList = adjustListOrder(infoEntityList);
        sheetMap.put("成员少于85", infoEntityList);
        sheetMap.put("成员多于85", longInfoEntityList);
        ExcelUtils.writeToLocal("F:\\投资公司信息-多成员.xls", sheetMap);
    }

    /**
     * 按照map size从大到小顺序,排列list
     *
     * @param list 待排列list
     * @return 排列后list
     */
    private List<Map<String, String>> adjustListOrder(List<Map<String, String>> list) {
        Map<Integer, List<Map<String, String>>> map = new TreeMap<>();
        for (Map<String, String> tempMap : list) {
            List<Map<String, String>> tempList;
            int size = tempMap.size();
            if (map.containsKey(size)) {
                tempList = map.get(size);
                tempList.add(tempMap);
            } else {
                tempList = new ArrayList<>();
                tempList.add(tempMap);
            }
            map.put(size, tempList);
        }
        List<Map<String, String>> returnList = new ArrayList<>();
        Set<Integer> keySet = map.keySet();
        for (int key : keySet) {
            List<Map<String, String>> tempList = map.get(key);
            returnList.addAll(0, tempList);
        }
        return returnList;
    }

    /**
     * 将超过指定长度的map切割为以指定最大长度的map
     *
     * @param rowMap 待切割map
     * @return 切割后的map list
     */
    private List<Map<String, String>> splitMap(Map<String, String> rowMap) {

        List<Map<String, String>> mapList = new ArrayList<>();

        int maxLength = 254;
        int basicLength = 11;
        //获取参数map的键
        Set<String> keySet = rowMap.keySet();
        List<String> keyList = new ArrayList<>(keySet);
        //获取前248列
        Map<String, String> firstMap = new LinkedHashMap<>();
        for (int rowI = 0; rowI < maxLength; rowI++) {
            String key = keyList.get(rowI);
            String value = rowMap.get(key);
            firstMap.put(key, value);
        }
        mapList.add(firstMap);
        for (int repeat = 1; repeat <= rowMap.size() / maxLength; repeat++) {
            Map<String, String> tempMap = new LinkedHashMap<>();
            for (int rowI = 0; rowI < basicLength; rowI++) {
                String key = keyList.get(rowI);
                String value = rowMap.get(key);
                tempMap.put(key, value);
            }
            int keyCount = 0;
            for (int kI = maxLength * repeat; kI < maxLength * (repeat + 1) && kI < rowMap.size(); kI++) {
                //获取超出长度的键
                String key = keyList.get(kI);
                //移除并获取对应的值
                String value = rowMap.get(key);
                //放入新的map中
                tempMap.put(keyList.get(keyCount + basicLength), value);
                keyCount++;
            }
            mapList.add(tempMap);
        }

        return mapList;
    }
}
